package com.example.tbag.healthcare.Activity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;

import com.example.tbag.healthcare.R;
import com.example.tbag.healthcare.Util.CallPhoneUtil;
import com.example.tbag.healthcare.Util.DatabaseHelper;
import com.example.tbag.healthcare.Util.ToastUtil;

import java.util.List;

import butterknife.BindViews;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Tbag on 2017/4/21.
 */

public class BedActivity extends AppCompatActivity {
    @BindViews({R.id.edit1, R.id.edit2, R.id.edit3, R.id.edit4, R.id.edit5, R.id.edit6, R.id.edit7, R.id.edit8, R.id.edit9})
    public List<EditText> editTexts;
    private int id;
    private DatabaseHelper helper;
    

    @OnClick(R.id.right_btn)
    public void callForHelp() {
        CallPhoneUtil.call(this);
    }

    @OnClick(R.id.left_btn)
    public void save() {
        ContentValues cv = new ContentValues();
        int index = 0;
        for (EditText editText : editTexts) {
            String text = editText.getText().toString().trim();
            switch (index) {
                case 0:
                    if(text.isEmpty()) {
                        ToastUtil.show(this, "姓名不能为空");
                        return;
                    }
                    cv.put("name",text);
                    break;
                case 1:
                    if(text.isEmpty()) {
                        ToastUtil.show(this, "科室不能为空");
                        return;
                    }
                    cv.put("type",text);
                    break;
                case 2:
                    cv.put("givenMoney",text);
                    break;
                case 3:
                    if(text.isEmpty()) {
                        ToastUtil.show(this, "性别不能为空");
                        return;
                    }
                    cv.put("sex",text);
                    break;
                case 4:
                    if(text.isEmpty()) {
                        ToastUtil.show(this, "床号不能为空");
                        return;
                    }
                    cv.put("bedNum",text);
                    break;
                case 5:
                    cv.put("usedMoney",text);
                    break;
                case 6:
                    if(text.isEmpty()) {
                        ToastUtil.show(this, "年龄不能为空");
                        return;
                    }
                    cv.put("age",text);
                    break;
                case 7:
                    if(text.isEmpty()) {
                        ToastUtil.show(this, "管床医生不能为空");
                        return;
                    }
                    cv.put("doc",text);
                    break;
                case 8:
                    cv.put("leftMoney",text);
                    break;
            }
            index++;
        }
        helper.getWritableDatabase().update("people",cv,"id=?",new String[]{id+""});
        ToastUtil.show(this, "保存成功");
        new Handler(getMainLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {
                exit();
            }
        },800);
    }

    private void exit() {
        this.finish();
    }
    


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bed);
        ButterKnife.bind(this);
        id = getIntent().getIntExtra("id", 0);
        helper = new DatabaseHelper(this);
        queryPerson();
        watchMoney();
        
    }

    private void watchMoney() {
        editTexts.get(2).addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String  text = editTexts.get(5).getText().toString();
                if(!text.isEmpty()){
                    int a = Integer.parseInt(text);
                    if(s.toString().isEmpty()){
                        editTexts.get(8).setText("");
                        return;
                    }
                    int b = Integer.parseInt(s.toString().trim());
                    int c = b-a;
                    editTexts.get(8).setText(c+"");
                }
            }
        });
    }

    private void queryPerson() {
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.query("people", null, "id=?", new String[]{id + ""}, null, null, null);
        if (cursor == null) {
            ToastUtil.show(this, "出问题啦");
            this.finish();
            return;
        }
        cursor.moveToFirst();
        setInfo(cursor);
        cursor.close();
    }

    public void setInfo(Cursor cursor) {
        String name = cursor.getString(cursor.getColumnIndex("name"));
        String type = cursor.getString(cursor.getColumnIndex("type"));
        String givenMoney = cursor.getString(cursor.getColumnIndex("givenMoney"));
        String usedMoney = cursor.getString(cursor.getColumnIndex("usedMoney"));
        String leftMoney = cursor.getString(cursor.getColumnIndex("leftMoney"));
        String hostpitalNum = cursor.getString(cursor.getColumnIndex("doc"));
        String sex = cursor.getString(cursor.getColumnIndex("sex"));
        String age = cursor.getString(cursor.getColumnIndex("age"));
        String bedNum = cursor.getString(cursor.getColumnIndex("bedNum"));
        editTexts.get(0).setText(name);
        editTexts.get(1).setText(type);
        editTexts.get(3).setText(sex);
        editTexts.get(4).setText(bedNum);
        editTexts.get(6).setText(age);
        editTexts.get(7).setText(hostpitalNum);
        if (!TextUtils.isEmpty(givenMoney)) {
            editTexts.get(2).setText(givenMoney);
        }
        if (!TextUtils.isEmpty(usedMoney)) {
            editTexts.get(5).setText(usedMoney);
        }
        if (!TextUtils.isEmpty(leftMoney)) {
            editTexts.get(8).setText(leftMoney);
        }
    }
}
